package net.jxvtc.blogv1.controller;

import net.jxvtc.blogv1.domain.Authority;
import net.jxvtc.blogv1.domain.User;
import net.jxvtc.blogv1.service.AuthorityService;
import net.jxvtc.blogv1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @program: blogv1
 * @description: 主页控制器
 * @author: 蓝建平
 * @create: 2020-10-24 10:00
 **/
@Controller
public class MainController {
	@Autowired
	private UserService userService;

	@Autowired
	private AuthorityService authorityService;
	@GetMapping("/")
	public String root() {
		return "redirect:/index";
	}
	
	@GetMapping("/index")
	public String index() {
		return "index";
	}

	@GetMapping("/login")
	public String login() {
		return "login";
	}

	@GetMapping("/login-error")
	public String loginError(Model model) {
		model.addAttribute("loginError", true);
		model.addAttribute("errorMsg", "登录失败，用户名或者密码错误！");
		return "login";
	}
	
	@GetMapping("/register")
	public String register() {
		return "register";
	}

	@PostMapping("/register")
	public String registerUser(User user)
	{

		List<Authority> authorities = new ArrayList<>();
		authorities.add(authorityService.getAuthorityById(new Long(2)).get());
		user.setAuthorities(authorities);
		userService.registerUser(user);
		return "redirect:/login";



	}
	
}
